#include<bits/stdc++.h>
using namespace std;
#define int long long

struct node {
	string name;
	int year, month, day, idx;
} a[1000];

signed main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].name >> a[i].year >> a[i].month >> a[i].day;
		a[i].idx = i;
	}

	sort(a + 1, a + n + 1, [](node x, node y) {
		if (x.year != y.year)
			return x.year < y.year;
		else if (x.month != y.month)
			return x.month < y.month;
		else if (x.day != y.day)
			return x.day < y.day;
		else
			return x.idx > y.idx;
	});

	for (int i = 1; i <= n; i++) {
		cout << a[i].name << endl;
	}
	return 0;
}
